﻿// Sorting an array means to arrange its elements in increasing order. Write a program to sort an array. 
// Use the "selection sort" algorithm: Find the smallest element, move it at the first position, find the smallest 
// from the rest, move it at the second position, etc.

using System;

class SelectionSortForArray
{
    static void Main()
    {
        // get the length of the array
        Console.Write("Enter length of the array : ");
        string length = Console.ReadLine();
        int lengthOfArray;
        while (!int.TryParse(length, out lengthOfArray))
        {
            Console.Write("Invalid input. Enter again length for the array : ");
            length = Console.ReadLine();
        }

        decimal[] array = new decimal[lengthOfArray];

        for (int index = 0; index < lengthOfArray; index++)
        {
            // get the element of the array
            Console.Write("Enter element of the array : ");
            string element = Console.ReadLine();
            decimal elementOfArray;
            while (!decimal.TryParse(element, out elementOfArray))
            {
                Console.Write("Invalid input. Enter element again : ");
                element = Console.ReadLine();
            }
            array[index] = elementOfArray;
        }

        // make the sort
        for (int i = 0; i < lengthOfArray - 1; i++)
        {
            int startIndex = i;
            for (int j = i + 1; j < lengthOfArray; j++)
            {
                if (array[startIndex] > array[j])
                {
                    startIndex = j;
                }
            }
            decimal temporaryValue = array[startIndex];
            array[startIndex] = array[i];
            array[i] = temporaryValue;
        }

        // print the sorted array
        string resultString = "";
        for (int i = 0; i < lengthOfArray; i++)
        {
            // for the last element of the sequence don't put comma after the element
            if (i == lengthOfArray - 1)
            {
                resultString = resultString + array[i];
            }
            else
            {
                resultString = resultString + array[i] + ", ";
            }
        }
        Console.WriteLine("And the sequence with max sum is : {" + resultString + "}");
    }
}